package dealer.controls
{
	import dealer.Global;
	import dealer.events.CustomEvent;
	import dealer.models.HandlerLogin;
	import dealer.models.ModelsLogin;
	
	import flash.display.MovieClip;
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;

	public class ControlLogin extends MovieClip
	{
		private var viewModelsLogin:ModelsLogin;
		
		public function ControlLogin()
		{
			super();
			init();
		}
		/* comment function */
		private function init():void
		{
			/* setup login form */
			HandlerLogin.init(Global.LOGIN_FORM);
			/* get models login */
			viewModelsLogin=new ModelsLogin();
			viewModelsLogin.addEventListener(CustomEvent.ON_SUBMIT_LOGIN,loginStatus);
			HandlerLogin.btnSubmit.addEventListener(MouseEvent.CLICK,doSubmit);
			/* on select text */		
			HandlerLogin.txtUser.addEventListener(FocusEvent.FOCUS_IN,doSelectText);
			HandlerLogin.txtPass.addEventListener(FocusEvent.FOCUS_IN,doSelectText);
			/* on check select */			
			HandlerLogin.chkHK.addEventListener(MouseEvent.CLICK,onChecked);
			HandlerLogin.chkEN.addEventListener(MouseEvent.CLICK,onChecked);
			/* catch keyboard */
			HandlerLogin.txtUser.addEventListener(KeyboardEvent.KEY_DOWN,checkKey);
			HandlerLogin.txtPass.addEventListener(KeyboardEvent.KEY_DOWN,checkKey);
		}
		/* comment function */
		private function doSubmit(event:MouseEvent=null):void
		{
			if(HandlerLogin.txtUser.text==""){
				viewModelsLogin._message="Input your username!";
				viewModelsLogin.doCheckSubmitLogin();				
			}else if(HandlerLogin.txtPass.text==""){
				viewModelsLogin._message="Input your password!";
				viewModelsLogin.doCheckSubmitLogin();				
			}else{
				viewModelsLogin._message="Please wait . . .";
				trace("LANG: "+Global.LANG+"\nU: "+HandlerLogin.txtUser.text+"\nP: "+HandlerLogin.txtPass.text);
				viewModelsLogin.doSubmitLogin(HandlerLogin.txtUser.text);
			}
		}
		/* login status */
		private function loginStatus(event:CustomEvent):void
		{		
			HandlerLogin.txtMessage.text=event.message;
		}
		/* do select text status */
		private function doSelectText(event:FocusEvent):void
		{			
			HandlerLogin.txtMessage.text="";
		}
		/* on click check */
		private function onChecked(event:MouseEvent):void
		{			
			if(event.target.name==HandlerLogin.chkHK.name){
				event.target.gotoAndStop("check");
				HandlerLogin.chkEN.gotoAndStop("uncheck");
				Global.LANG="B";
				changeLanguageLabel(Global.LANG);
			}else{
				event.target.gotoAndStop("check");
				HandlerLogin.chkHK.gotoAndStop("uncheck");
				Global.LANG="E";
				changeLanguageLabel(Global.LANG);
			}			
		}
		/* change language label function */
		private function changeLanguageLabel(language:String):void
		{
			for(var i:int=0;i<HandlerLogin.lbArr.length;i++){
				HandlerLogin.lbArr[i].gotoAndStop(language);
			}
		}
		/* comment function */
		private function checkKey(event:KeyboardEvent):void
		{
			if(event.keyCode==13){
				doSubmit();
			}
		}
		
	}
	/* End Class */
}